//  Inside Microsoft Exchange 2007 Web Services
//  Chapter 8
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using ProxyHelpers.EWS;
using System.Xml.Serialization;
using System.IO;

namespace FunWithDateTimeKind
{
    class Program
    {
        static void Main(string[] args)
        {
            FunWithDateTimeKind();
        }

        /// <summary>
        /// Show the result of using various DateTimeKind values (Listing 8-5)
        /// </summary>
        public static void FunWithDateTimeKind()
        {
            // Create a new DateTime instance, explicitly set it's .Kind value to
            // Unspecified.
            //
            DateTime dateTime = new DateTime(2007, 01, 01, 08, 15, 00,
                                             DateTimeKind.Unspecified);

            // Create a CalendarItem type using the Unspecified DateTime instance to
            // produce an un-timezoned dateTime string.
            //
            CalendarItemType calItemUnspecified = new CalendarItemType();
            calItemUnspecified.Start = dateTime;
            calItemUnspecified.End = calItemUnspecified.Start.AddHours(1);
            calItemUnspecified.StartSpecified = true;
            calItemUnspecified.EndSpecified = true;
            SerializeToConsole("DateTimeKind.Unspecified", calItemUnspecified);

            // Call .ToLocalTime() to produce a timezoned string with an offset matching
            // the time zone of the current operating system. (e.g. PST in this example)
            //
            CalendarItemType calItemLocal = new CalendarItemType();
            calItemLocal.Start = dateTime.ToLocalTime();
            calItemLocal.End = calItemLocal.Start.AddHours(1);
            calItemLocal.StartSpecified = calItemLocal.EndSpecified = true;
            SerializeToConsole("DateTimeKind.Local", calItemLocal);

            // Call .ToUniversalTime() to produce a timezoned string with the canonical
            // 'Z' representation.
            //
            CalendarItemType calItemUTC = new CalendarItemType();
            calItemUTC.Start = dateTime.ToUniversalTime();
            calItemUTC.End = calItemUTC.Start.AddHours(1);
            calItemUTC.StartSpecified = calItemUTC.EndSpecified = true;
            SerializeToConsole("DateTimeKind.UTC", calItemUTC);

        }

        /// <summary>
        /// Serializes the supplied CalendarItemType instance to the Console
        /// </summary>
        /// <param name="header">Text descriptive header to write out before instance</param>
        /// <param name="calendarItem">CalendarItemType to serialize</param>
        /// 
        public static void SerializeToConsole(string header, CalendarItemType calendarItem)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CalendarItemType));

            Console.WriteLine(header);
            Console.WriteLine("--------------------------");
            using (Stream outStream = Console.OpenStandardOutput())
            {
                serializer.Serialize(outStream, calendarItem);
            }
            Console.WriteLine();
            Console.WriteLine();
        }

    }
}
